from threading import Timer

from PyQt5.QtWidgets import (QWidget, QFrame, QMainWindow, QPushButton, QMessageBox)
from PyQt5.QtCore import Qt, pyqtSlot

import py3func
from stickybtn import ButtonFactory
from cui import Qt_set_bkground_img
from parsermk.xmlparser import XmlParser

LOG = py3func.make_logging(__file__)

class KeyWnd(QMainWindow):
    def __init__(self, file_xml, zoom_geom):
        super().__init__()

        # @attribute
        self.xml_parser = XmlParser()
        # self.btns_parser = None  # current page's btn-list parser
        self.zoom_x, self.zoom_y = 1, 1
        self.str_qss = ""

        # @initialize
        self.xml_parser.load_xml(file_xml)
        self._set_window_facade(zoom_geom)
        default_page = self.xml_parser.get_wnd_default_page()
        self.make_page(default_page)

        self.show()

        # 启动PLC等按键队列的状态监听
        self.listen_btns_output()

    @pyqtSlot(str)
    def vkb_show(self):
        self.show()

    @pyqtSlot(str)
    def vkb_hide(self):  # param: dbus_msg
        # LOG.debug("DBus Message called: %s" % dbus_msg)
        self.hide()

    @pyqtSlot(str)
    def vkb_quit(self):
        # QMetaObject.invokeMethod(QCoreApplication.instance(), 'quit')
        self.close()

    @pyqtSlot(str)
    def vkb_lock(self):
        pass

    @pyqtSlot(str)
    def vkb_unlock(self):
        pass

    def _set_window_facade(self, zoom_geometry):
        pos_x, pos_y, size_w, size_h = self.xml_parser.get_wnd_geometry()
        if zoom_geometry:
            pos_x, pos_y = zoom_geometry[0], zoom_geometry[1]
            if zoom_geometry[2]:
                self.zoom_x = zoom_geometry[2] / size_w
            if zoom_geometry[3]:
                self.zoom_y = zoom_geometry[3] / size_h

        # self.setGeometry()
        self.move(pos_x, pos_y)  # 锁定位置
        self.setFixedSize(size_w * self.zoom_x, 
                          size_h * self.zoom_y)  # 锁定尺寸
        # self.showFullScreen()
        # self.setWindowTitle('软面板示例')
        self.setWindowFlags(Qt.X11BypassWindowManagerHint |  # 隐藏标题栏  Tooltip/SplashScreen/FramelessWindowHint
                            # Qt.CustomizeWindowHint |
                            # Qt.WindowStaysOnTopHint |  # 窗口置顶
                            Qt.WindowDoesNotAcceptFocus)  # 不允许捕获焦点

        # 应用QSS样式表
        qss_file = self.xml_parser.get_wnd_qssfile()
        if qss_file:
            with open(qss_file, "r") as fp:
                self.str_qss = fp.read()
            self.setStyleSheet(self.str_qss)

    def make_page(self, page_id):
        pnl_page = QFrame(self)
        pnl_page.resize(self.size())  # page的尺寸默认等同于窗口size
        Qt_set_bkground_img(pnl_page, self.xml_parser.get_page_imgfile(page_id))
        self._make_buttons(pnl_page, page_id)

    def _make_buttons(self, parent_widget, page_id):
        btns_parser = self.xml_parser.get_btn_parser(page_id)
        factory = ButtonFactory(btns_parser, self.zoom_x, self.zoom_y, self.str_qss)
        for btn_id, _ in btns_parser.data_items():
            factory.get_btn(parent_widget, btn_id)

    def listen_btns_output(self):
        """ listen the queue of buttons need to update by status """
        timer = Timer(1, self.listen_btns_output)

        # for btn in Set_Btns_Listening:
        #     pass

        # 递归调用
        # self.listen_btns_output()
